package com.panopset.gui.container;

import javax.swing.JComponent;

import com.panopset.centralen.UniqueKey;
import com.panopset.swing.Field;
import com.panopset.swing.FieldDefaultHardCoded;
import com.panopset.swing.FieldValue;
import com.panopset.swing.Scaffolding;

public abstract class PanNoBorderField<T extends JComponent> extends Field {

	@Override
	protected abstract boolean isValid(final String valueToCheck);

	/**
	 * Tool tip text.
	 */
	protected final String ptoolTipText;

	/**
	 * @param defaultValue
	 *            Default value.
	 * 
	 */
	public PanNoBorderField(final String defaultValue, final String toolTipText) {
		this(defaultValue, toolTipText, true);
	}

	/**
	 * @param defaultValue
	 *            Default value.
	 * 
	 */
	public PanNoBorderField(final String defaultValue, final String toolTipText, boolean persistent) {
		super(persistent);
		setFieldDefault(new FieldDefaultHardCoded(defaultValue));
		ptoolTipText = toolTipText;
		if (persistent) {
			final String key = UniqueKey.generateStackHashKey();
			setFieldValue(new FieldValue() {
				/**
				 * @return Value.
				 */
				public String getValue() {
					String s = Scaffolding.s.get(key);
					if (s == null) {
						return null;
					}
					return s;
				}

				/**
				 * @param value
				 *            Value.
				 * @return Same value.
				 */
				public void setValue(final String value) {
					Scaffolding.s.put(key, value);
				}
			});
		}
	}

	/**
	 * @return Inner component.
	 */
	protected abstract T createComponent();

	/**
	 * Inner component gui.
	 */
	private T gui;

	/**
	 * @return inner component.
	 */
	public final T getInnerComponent() {
		if (gui == null) {
			gui = createComponent();
			gui.setToolTipText(ptoolTipText);
		}
		return gui;
	}
}
